Navigation Methods
You can trigger live navigation in two ways:From the client
Pass either
patch={url} or navigate={url} to the Phoenix.Component.link/1 component.Patch vs Navigate
Here is a quick breakdown:<.link href={...}>andredirect/2are HTTP-based, work everywhere, and perform full page reloads<.link navigate={...}>andpush_navigate/2work across LiveViews in the same session. They mount a new LiveView while keeping the current layout<.link patch={...}>andpush_patch/2update the current LiveView and send only the minimal diff while also maintaining the scroll position
Using Patch
The “patch” operations must be used when you want to navigate to the current LiveView, simply updating the URL and the current parameters, without mounting a new LiveView.handle_params/3 callback is invoked and the minimal set of changes are sent to the client.
Using Navigate
The “navigate” operations must be used when you want to dismount the current LiveView and mount a new one. You can only “navigate” between LiveViews in the same session.phx-loading class is added to the LiveView, which can be used to indicate to the user a new page is being loaded.
The handle_params/3 Callback
The handle_params/3 callback is invoked:
- After
mount/3and before the initial render - Every time
<.link patch={...}>orpush_patch/2are used
Example: Live Sorting
Imagine you have aUserTable LiveView to show all users in the system:
You must never trust the received params. Always validate the user input and change the state accordingly.
When to Use mount/3 vs handle_params/3
Generally speaking, data should always be loaded on mount/3, since mount/3 is invoked once per LiveView life-cycle. Only the params you expect to be changed via <.link patch={...}> or push_patch/2 must be loaded on handle_params/3.
Example: Blog with Paginated Comments
For a blog post at /blog/posts/:post_id with paginated comments at /blog/posts/:post_id?page=X:
- Access
"post_id"onmount/3to load the post - Access the page of comments on
handle_params/3to handle pagination
Replace Page Address
LiveView allows the current browser URL to be replaced without polluting the browser’s history. This is useful when you want certain events to change the URL but without adding to the history.Multiple LiveViews in the Same Page
LiveView allows you to have multiple LiveViews in the same page by callingPhoenix.Component.live_render/3 in your templates. However, only the LiveViews defined directly in your router can use the Live Navigation functionality described here.
This is important because LiveViews work closely with your router, guaranteeing you can only navigate to known routes.
API Reference
push_patch/2
Navigates to the same LiveView with new parameters.
:to- The path to navigate to (required):replace- Whether to replace the current history entry (default:false)
push_navigate/2
Navigates to a different LiveView in the same session.
:to- The path to navigate to (required):replace- Whether to replace the current history entry (default:false)
Best Practices
- Use patch for same LiveView: When updating parameters within the same LiveView (filtering, sorting, pagination)
- Use navigate for different LiveViews: When moving to a completely different page
- Validate params: Always validate parameters in
handle_params/3- never trust user input - Load static data in mount: Load data that doesn’t change based on URL params in
mount/3 - Keep URLs bookmarkable: Store important UI state in URL parameters to make pages shareable